package com.dg11185.nearshop.view;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.nearshop.R;
import com.dg11185.nearshop.view.adapter.SimpleListAdapter;

/**
 * 
 * @author wanliang527
 * @date 2014-4-24
 */
public class TwoLevelCatalogView extends LinearLayout {

    private ListView firstLevelCatalogView;// 一级目录View
    private ListView secondLevelCatalogView;// 二级目录View
    private ArrayList<String> firstLevelNames = new ArrayList<String>();// 一级目录名称
    private SparseArray<LinkedList<String>> secondLevelNames = new SparseArray<LinkedList<String>>();// 二级目录名称

    private LinkedList<String> childrenItem = new LinkedList<String>();

    private SimpleListAdapter firstLevelCatalogAdapter;// 一级目录列表数据适配器
    private SimpleListAdapter secondLevelCatalogAdapter;// 二级目录列表数据适配器
    private OnSelectListener mOnSelectListener;// 条目选中监听器
    private int seltFirLevPos = 0;// 一级目录已选中的条目
    private int seltSecLevPos = 0;// 二级目录已选中的条目
    private String showString = "不限";

    // private Drawable normalBg1, selectedBg1, normalBg2, selectedBg2;

    public void setNormalBg1(Drawable normalBg1) {
        firstLevelCatalogAdapter.setNormalItemBg(normalBg1);
    }

    public void setSelectedBg1(Drawable selectedBg1) {
        firstLevelCatalogAdapter.setSelectedItemBg(selectedBg1);
    }

    public void setNormalBg2(Drawable normalBg2) {
        secondLevelCatalogAdapter.setNormalItemBg(normalBg2);
    }

    public void setSelectedBg2(Drawable selectedBg2) {
        secondLevelCatalogAdapter.setSelectedItemBg(selectedBg2);
    }

    public void setSecListBg(int color) {
        if (secondLevelCatalogView != null) {
            secondLevelCatalogView.setBackgroundColor(getResources().getColor(
                    color));
        }
    }

    /**
     * 
     * @param context
     * @param groups
     *            一级目录名
     * @param children
     *            二级目录名
     */
    public TwoLevelCatalogView(Context context, ArrayList<String> groups,
            SparseArray<LinkedList<String>> children) {
        super(context);
        setData(groups, children);
        init(context);
    }

    public TwoLevelCatalogView(Context context, AttributeSet attrs,
            ArrayList<String> groups, SparseArray<LinkedList<String>> children) {
        super(context, attrs);
        setData(groups, children);
        init(context);
    }

    private void setData(ArrayList<String> groups,
            SparseArray<LinkedList<String>> children) {
        this.firstLevelNames = groups;
        this.secondLevelNames = children;
    }

    private void init(final Context context) {
        this.setOrientation(LinearLayout.VERTICAL);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_two_level_catalog, this, true);
        firstLevelCatalogView = (ListView) findViewById(R.id.list_v_first_lev);
        secondLevelCatalogView = (ListView) findViewById(R.id.list_v_second_lev);
        setBackgroundColor(context.getResources().getColor(
                R.color.ex_transparent));

        firstLevelCatalogAdapter = new SimpleListAdapter(context,
                firstLevelNames, R.color.ex_list_selected,
                R.color.ex_list_normal);
        firstLevelCatalogAdapter.setSelectedPosition(seltSecLevPos, false);
        firstLevelCatalogView.setAdapter(firstLevelCatalogAdapter);
        firstLevelCatalogAdapter
                .setOnItemClickListener(new SimpleListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        seltFirLevPos = position;
                        if (position < secondLevelNames.size()) {
                            if (secondLevelNames.get(position).size() == 0) {
                                // 如果选定的一级目录所对应二级目录列表大小为空的话
                                // 直接返回
                                if (mOnSelectListener != null) {
                                    mOnSelectListener.getValue(
                                            TwoLevelCatalogView.this, position,
                                            -1, firstLevelNames.get(position));
                                }
                            }
                            childrenItem.clear();
                            childrenItem.addAll(secondLevelNames.get(position));
                            secondLevelCatalogAdapter.notifyDataSetChanged();
                        }
                    }
                });

        if (seltFirLevPos < secondLevelNames.size())
            childrenItem.addAll(secondLevelNames.get(seltFirLevPos));
        secondLevelCatalogAdapter = new SimpleListAdapter(context,
                childrenItem, R.drawable.sec_lev_catalog_list_item_selected_bg,
                R.color.ex_list_selected);
        secondLevelCatalogAdapter.setTextSize(15);
        secondLevelCatalogAdapter.setSelectedPosition(seltSecLevPos, false);
        secondLevelCatalogView.setAdapter(secondLevelCatalogAdapter);
        secondLevelCatalogAdapter
                .setOnItemClickListener(new SimpleListAdapter.OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, final int position) {
                        seltSecLevPos = position;
                        if (seltSecLevPos == 0) {
                            // 如果二级列表选择的是第一个不限类型，就返回对应的一级列表的标题
                            showString = firstLevelNames.get(seltFirLevPos);
                        } else {
                            // 否则，返回二级列表的标题
                            showString = childrenItem.get(position);
                        }
                        if (mOnSelectListener != null) {
                            mOnSelectListener.getValue(
                                    TwoLevelCatalogView.this, seltFirLevPos,
                                    position, showString);
                        }

                    }
                });

    }

    public String getShowText() {
        return showString;
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        public void getValue(View view, int firLevPos, int secLevPos,
                String showText);
    }

}
